import { defineStore } from "pinia";
import { ref } from "vue";
import type { Ref } from 'vue'
interface iUser {
    token: string
    username: string
    isLogin: boolean
}
export const useUserStore = defineStore('user', () => {
    //存放参数  这个参数在登录成功后res返回体里面
    const token: Ref<string> = ref(localStorage.getItem('token')||'')
    const username: Ref<string> = ref(localStorage.getItem('username')||'')
    const isLogin: Ref<boolean> = ref(localStorage.getItem('isLogin')=='true')//默认设置未登录
    function changeUserState(userInfo:iUser){
        token.value=userInfo.token
        username.value=userInfo.username
        isLogin.value=userInfo.isLogin
        //把数据存在本地
        localStorage.setItem('token',userInfo.token)
        localStorage.setItem('username',userInfo.username)
        localStorage.setItem('isLogin',String(userInfo.isLogin))

    }
    return {
        token,
        username,
        isLogin,
        changeUserState
    }
})
